package com.tiantian.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.common.core.domain.page.TableDataInfo;
import com.tiantian.common.excel.utils.ExcelUtil;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.satoken.domain.controller.BaseController;
import com.tiantian.system.domain.entity.SysOperLog;
import com.tiantian.system.domain.vo.OperType;
import com.tiantian.system.domain.vo.SysLogininforVo;
import com.tiantian.system.service.SysOperLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作日志记录
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/log/operLog")
@SaCheckPermission("log")
public class SysOperLogController extends BaseController {

    private final SysOperLogService operLogService;

    @GetMapping("/typeList")
    public ResponseResult<List<OperType>> getOperTypeList() {
        List<OperType> list = Arrays.stream(BusinessType.values())
                .map(value -> OperType.builder().businessType(value.getNumber()).description(value.getDescription()).build())
                .collect(Collectors.toList());
        return ResponseResult.ok(list);
    }

    @GetMapping("/export")
    @Log(businessType = BusinessType.EXP_LOG, isSaveResponseData = false, isSaveRequestData = false)
    public void exportLog(HttpServletResponse response){
        List<SysOperLog> sysOperLogs = operLogService.selectOperLogList(null);
        ExcelUtil.exportExcel(sysOperLogs, "登录日志", SysOperLog.class, response);
    }

    /**
     * 获取操作日志记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog) {
        startPage();
        List<SysOperLog> sysOperLogs = operLogService.selectOperLogList(operLog);
        return getDataTable(sysOperLogs);
    }

    /**
     * 根据id查询日志数据
     *
     * @param operId 日志id
     */
    @GetMapping("/{operId}")
    public ResponseResult<SysOperLog> getOperLogById(@PathVariable Long operId) {
        SysOperLog sysOperLog = operLogService.selectOperLogById(operId);
        return ResponseResult.ok(sysOperLog);
    }

    /**
     * 批量删除操作日志记录
     *
     * @param operIds 日志ids
     */
    @DeleteMapping("/{operIds}")
    @Log(businessType = BusinessType.DEL_LOG)
    public ResponseResult<Void> remove(@PathVariable Long[] operIds) {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    /**
     * 清理操作日志记录
     */
    @DeleteMapping("/clean")
    @Log(businessType = BusinessType.CLEAR_LOG)
    public ResponseResult<Void> clean() {
        operLogService.cleanOperLog();
        return ResponseResult.ok();
    }
}
